package com.app.controller;

import com.app.mapper.akesai.ComRecruitersAkesaiMapper;
import com.app.mapper.akesai.EnterpriseAkesaiMapper;
import com.app.mapper.akesai.UserAkesaiMapper;
import com.app.mapper.dunhuang.ComRecruitersDunhuangMapper;
import com.app.mapper.dunhuang.EnterpriseDunhuangMapper;
import com.app.mapper.dunhuang.UserDunhuangMapper;
import com.app.mapper.guazhou.ComRecruitersGuazhouMapper;
import com.app.mapper.guazhou.EnterpriseGuazhouMapper;
import com.app.mapper.guazhou.UserGuazhouMapper;
import com.app.mapper.jinta.ComRecruitersJintaMapper;
import com.app.mapper.jinta.EnterpriseJintaMapper;
import com.app.mapper.jinta.UserJintaMapper;
import com.app.mapper.subei.ComRecruitersSubeiMapper;
import com.app.mapper.subei.EnterpriseSubeiMapper;
import com.app.mapper.subei.UserSubeiMapper;
import com.app.mapper.suzhou.ComRecruitersSuzhouMapper;
import com.app.mapper.suzhou.EnterpriseSuzhouMapper;
import com.app.mapper.suzhou.UserSuzhouMapper;
import com.app.mapper.yumen.ComRecruitersYumenMapper;
import com.app.mapper.yumen.EnterpriseYumenMapper;
import com.app.mapper.yumen.UserYumenMapper;
import com.app.model.AdminUserModel;
import com.app.model.ComRecruitersModel;
import com.app.model.UserModel;
import com.app.service.AdminUserService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.ComRecruitersVo;
import com.google.common.collect.Maps;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Controller
public class PersonnelController {

    @Autowired
    AdminUserService adminUserService;
    @Autowired
    ComRecruitersSuzhouMapper comRecruitersSuzhouMapper;
    @Autowired
    ComRecruitersJintaMapper comRecruitersJintaMapper;
    @Autowired
    ComRecruitersDunhuangMapper comRecruitersDunhuangMapper;
    @Autowired
    ComRecruitersGuazhouMapper comRecruitersGuazhouMapper;
    @Autowired
    ComRecruitersYumenMapper comRecruitersYumenMapper;
    @Autowired
    ComRecruitersSubeiMapper comRecruitersSubeiMapper;
    @Autowired
    ComRecruitersAkesaiMapper comRecruitersAkesaiMapper;
    @Autowired
    EnterpriseGuazhouMapper enterpriseGuazhouMapper;
    @Autowired
    EnterpriseSuzhouMapper enterpriseSuzhouMapper;
    @Autowired
    EnterpriseJintaMapper enterpriseJintaMapper;
    @Autowired
    EnterpriseDunhuangMapper enterpriseDunhuangMapper;
    @Autowired
    EnterpriseYumenMapper enterpriseYumenMapper;
    @Autowired
    EnterpriseSubeiMapper enterpriseSubeiMapper;
    @Autowired
    EnterpriseAkesaiMapper enterpriseAkesaiMapper;
    @Autowired
    UserSuzhouMapper userSuzhouMapper;
    @Autowired
    UserJintaMapper userJintaMapper;
    @Autowired
    UserDunhuangMapper userDunhuangMapper;
    @Autowired
    UserGuazhouMapper userGuazhouMapper;
    @Autowired
    UserYumenMapper userYumenMapper;
    @Autowired
    UserSubeiMapper userSubeiMapper;

    @Autowired
    UserAkesaiMapper userAkesaiMapper;


    @GetMapping("/getPersonnelList")
    public Result<Object> getPersonnelList(
            int page, int limit,
            @RequestParam(required = false) String keyWord,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer sceneType,
            @RequestParam(required = false) Integer enrollId,
            @RequestParam(required = false) Integer addressId,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel adminUser = adminUserService.getUserById(Integer.parseInt(userId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        ComRecruitersModel model = new ComRecruitersModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setKeyWord(keyWord);
        model.setStartTime(startTime);
        model.setEndTime(endTime);
        model.setAddressId(addressId);
        if (enrollId != null) {
            model.setComId(enrollId);
        }
        UserModel user = new UserModel();
        user.setRRegisterState(sceneType);
        model.setUser(user);
        ComRecruitersVo vo = new ComRecruitersVo();

        if (addressId != null && addressId.equals(1)) {
            vo = suzhouComRecruiters(model);
        } else if (addressId != null && addressId.equals(2)) {
            vo = jintaComRecruiters(model);
        } else if (addressId != null && addressId.equals(3)) {
            vo = dunhuangComRecruiters(model);
        } else if (addressId != null && addressId.equals(4)) {
            vo = guazhouComRecruiters(model);
        } else if (addressId != null && addressId.equals(5)) {
            vo = yumenComRecruiters(model);
        } else if (addressId != null && addressId.equals(6)) {
            vo = subeiComRecruiters(model);
        } else if (addressId != null && addressId.equals(7)) {
            vo = akesaiComRecruiters(model);
        } else {
            int suzhouCount = comRecruitersSuzhouMapper.getModelListCount(model);
            int jintaCount = comRecruitersJintaMapper.getModelListCount(model);
            int dunhuangCount = comRecruitersDunhuangMapper.getModelListCount(model);
            int guazhouCount = comRecruitersGuazhouMapper.getModelListCount(model);
            int yumenCount = comRecruitersYumenMapper.getModelListCount(model);
            int subeiCount = comRecruitersSubeiMapper.getModelListCount(model);
            int akesaiCount = comRecruitersAkesaiMapper.getModelListCount(model);
            int count = jintaCount + suzhouCount + dunhuangCount + guazhouCount + yumenCount + subeiCount + akesaiCount;
            int pageCount = suzhouCount / limit;
            int remainderResult = suzhouCount % limit;
            if (pageCount + 1 >= page) {
                vo = suzhouComRecruiters(model);
            }
            if (page > pageCount) {
                if (page == pageCount + 1 && remainderResult > 0) {
                    model.setLimit(limit - remainderResult);
                    model.setPage(page - pageCount);
                    vo.getList().addAll(jintaComRecruiters(model).getList());
                }
                if (page > pageCount + 1) {
                    model.setLimit(limit);
                    model.setPage((page - (pageCount + 2)) * limit);
                    vo = jintaComRecruiters(model);
                }

                int pageCount2 = (suzhouCount + jintaCount) / limit;
                int remainderResult2 = (suzhouCount + jintaCount) % limit;
                if (page > pageCount2) {
                    if (page == pageCount2 + 1 && remainderResult2 > 0) {
                        model.setLimit(limit - remainderResult2);
                        model.setPage(page - pageCount2);
                        vo.getList().addAll(dunhuangComRecruiters(model).getList());
                    }
                    if (page > pageCount2 + 1) {
                        model.setLimit(limit);
                        model.setPage((page - (pageCount2 + 2)) * limit);
                        vo = dunhuangComRecruiters(model);
                    }
                    int pageCount3 = (suzhouCount + jintaCount + dunhuangCount) / limit;
                    int remainderResult3 = (suzhouCount + jintaCount + dunhuangCount) % limit;
                    if (page > pageCount3) {
                        if (page == pageCount3 + 1 && remainderResult3 > 0) {
                            model.setLimit(limit - remainderResult3);
                            model.setPage(page - pageCount3);
                            vo.getList().addAll(guazhouComRecruiters(model).getList());
                        }
                        if (page > pageCount3 + 1) {
                            model.setLimit(limit);
                            model.setPage((page - (pageCount3 + 2)) * limit);
                            vo = guazhouComRecruiters(model);
                        }
                        int pageCount4 = (suzhouCount + jintaCount + dunhuangCount + guazhouCount) / limit;
                        int remainderResult4 = (suzhouCount + jintaCount + dunhuangCount + guazhouCount) % limit;
                        if (page > pageCount4) {
                            if (page == pageCount4 + 1 && remainderResult4 > 0) {
                                model.setLimit(limit - remainderResult4);
                                model.setPage(page - pageCount4);
                                vo.getList().addAll(yumenComRecruiters(model).getList());
                            }
                            if (page > pageCount4 + 1) {
                                model.setLimit(limit);
                                model.setPage((page - (pageCount4 + 2)) * limit);
                                vo = yumenComRecruiters(model);
                            }
                            int pageCount5 = (suzhouCount + jintaCount + dunhuangCount + guazhouCount + yumenCount) / limit;
                            int remainderResult5 = (suzhouCount + jintaCount + dunhuangCount + guazhouCount + yumenCount) % limit;
                            if (page > pageCount5) {
                                if (page == pageCount5 + 1 && remainderResult5 > 0) {
                                    model.setLimit(limit - remainderResult5);
                                    model.setPage(page - pageCount5);
                                    vo.getList().addAll(subeiComRecruiters(model).getList());
                                }
                                if (page > pageCount5 + 1) {
                                    model.setLimit(limit);
                                    model.setPage((page - (pageCount5 + 2)) * limit);
                                    vo = subeiComRecruiters(model);
                                }
                                int pageCount6 = (suzhouCount + jintaCount + dunhuangCount + guazhouCount + yumenCount + subeiCount) / limit;
                                int remainderResult6 = (suzhouCount + jintaCount + dunhuangCount + guazhouCount + yumenCount + subeiCount) % limit;
                                if (page > pageCount6) {
                                    if (page == pageCount6 + 1 && remainderResult6 > 0) {
                                        model.setLimit(limit - remainderResult6);
                                        model.setPage(page - pageCount6);
                                        vo.getList().addAll(akesaiComRecruiters(model).getList());
                                    }
                                    if (page > pageCount6 + 1) {
                                        model.setLimit(limit);
                                        model.setPage((page - (pageCount6 + 2)) * limit);
                                        vo = akesaiComRecruiters(model);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            vo.setCount(count);

        }

        return Result.success(vo);
    }

    public static void main(String[] args) {
        int pageCount = 506 / 10;
        int remainderResult = 506 % 10;
        System.out.println(pageCount);
        System.out.println(remainderResult);
    }

    /**
     * 肃州区
     *
     * @param model
     * @return
     */
    public ComRecruitersVo suzhouComRecruiters(ComRecruitersModel model) {
        List<ComRecruitersModel> modelList = comRecruitersSuzhouMapper.getComRecruitersList(model);
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setEnterprise(enterpriseSuzhouMapper.selectById(modelList.get(i).getComId()));
            if (modelList.get(i).getRRegisterState() == 4) {
                modelList.get(i).setStateText("已拒绝");
            } else if (modelList.get(i).getRRegisterState() == 3) {
                modelList.get(i).setStateText("待审核");
            } else if (modelList.get(i).getRRegisterState() == 9) {
                modelList.get(i).setStateText("已通过");
            }

            UserModel userModel = userSuzhouMapper.getModelByRid(modelList.get(i).getId());
            modelList.get(i).setUser(userModel);
            modelList.get(i).setAddressId(1);
            modelList.get(i).setAddressText("肃州区");
        }
        int count = comRecruitersSuzhouMapper.getModelListCount(model);
        ComRecruitersVo vo = new ComRecruitersVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    /**
     * 金塔
     *
     * @param model
     * @return
     */
    public ComRecruitersVo jintaComRecruiters(ComRecruitersModel model) {
        List<ComRecruitersModel> modelList = comRecruitersJintaMapper.getComRecruitersList(model);
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setEnterprise(enterpriseJintaMapper.selectById(modelList.get(i).getComId()));
            if (modelList.get(i).getRRegisterState() == 4) {
                modelList.get(i).setStateText("已拒绝");
            } else if (modelList.get(i).getRRegisterState() == 3) {
                modelList.get(i).setStateText("待审核");
            } else if (modelList.get(i).getRRegisterState() == 9) {
                modelList.get(i).setStateText("已通过");
            }


            UserModel userModel = userJintaMapper.getModelByRid(modelList.get(i).getId());
            modelList.get(i).setUser(userModel);
            modelList.get(i).setAddressId(2);
            modelList.get(i).setAddressText("金塔县");
        }
        int count = comRecruitersJintaMapper.getModelListCount(model);
        ComRecruitersVo vo = new ComRecruitersVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    /**
     * 敦煌
     *
     * @param model
     * @return
     */
    public ComRecruitersVo dunhuangComRecruiters(ComRecruitersModel model) {
        List<ComRecruitersModel> modelList = comRecruitersDunhuangMapper.getComRecruitersList(model);
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setEnterprise(enterpriseDunhuangMapper.selectById(modelList.get(i).getComId()));
            if (modelList.get(i).getRRegisterState() == 4) {
                modelList.get(i).setStateText("已拒绝");
            } else if (modelList.get(i).getRRegisterState() == 3) {
                modelList.get(i).setStateText("待审核");
            } else if (modelList.get(i).getRRegisterState() == 9) {
                modelList.get(i).setStateText("已通过");
            }


            UserModel userModel = userDunhuangMapper.getModelByRid(modelList.get(i).getId());
            modelList.get(i).setUser(userModel);
            modelList.get(i).setAddressId(3);
            modelList.get(i).setAddressText("敦煌市");
        }
        int count = comRecruitersDunhuangMapper.getModelListCount(model);
        ComRecruitersVo vo = new ComRecruitersVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    /**
     * 瓜州
     *
     * @param model
     * @return
     */
    public ComRecruitersVo guazhouComRecruiters(ComRecruitersModel model) {
        List<ComRecruitersModel> modelList = comRecruitersGuazhouMapper.getComRecruitersList(model);
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setEnterprise(enterpriseGuazhouMapper.selectById(modelList.get(i).getComId()));
            if (modelList.get(i).getRRegisterState() == 4) {
                modelList.get(i).setStateText("已拒绝");
            } else if (modelList.get(i).getRRegisterState() == 3) {
                modelList.get(i).setStateText("待审核");
            } else if (modelList.get(i).getRRegisterState() == 9) {
                modelList.get(i).setStateText("已通过");
            }

            UserModel userModel = userGuazhouMapper.getModelByRid(modelList.get(i).getId());
            modelList.get(i).setUser(userModel);
            modelList.get(i).setAddressId(4);
            modelList.get(i).setAddressText("瓜州县");
        }
        int count = comRecruitersGuazhouMapper.getModelListCount(model);
        ComRecruitersVo vo = new ComRecruitersVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    /**
     * 玉门
     *
     * @param model
     * @return
     */
    public ComRecruitersVo yumenComRecruiters(ComRecruitersModel model) {
        List<ComRecruitersModel> modelList = comRecruitersYumenMapper.getComRecruitersList(model);
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setEnterprise(enterpriseYumenMapper.selectById(modelList.get(i).getComId()));
            if (modelList.get(i).getRRegisterState() == 4) {
                modelList.get(i).setStateText("已拒绝");
            } else if (modelList.get(i).getRRegisterState() == 3) {
                modelList.get(i).setStateText("待审核");
            } else if (modelList.get(i).getRRegisterState() == 9) {
                modelList.get(i).setStateText("已通过");
            }

            UserModel userModel = userYumenMapper.getModelByRid(modelList.get(i).getId());
            modelList.get(i).setUser(userModel);
            modelList.get(i).setAddressId(5);
            modelList.get(i).setAddressText("玉门市");
        }
        int count = comRecruitersYumenMapper.getModelListCount(model);
        ComRecruitersVo vo = new ComRecruitersVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    /**
     * 肃北
     *
     * @param model
     * @return
     */
    public ComRecruitersVo subeiComRecruiters(ComRecruitersModel model) {
        List<ComRecruitersModel> modelList = comRecruitersSubeiMapper.getComRecruitersList(model);
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setEnterprise(enterpriseSubeiMapper.selectById(modelList.get(i).getComId()));
            if (modelList.get(i).getRRegisterState() == 4) {
                modelList.get(i).setStateText("已拒绝");
            } else if (modelList.get(i).getRRegisterState() == 3) {
                modelList.get(i).setStateText("待审核");
            } else if (modelList.get(i).getRRegisterState() == 9) {
                modelList.get(i).setStateText("已通过");
            }

            UserModel userModel = userSubeiMapper.getModelByRid(modelList.get(i).getId());
            modelList.get(i).setUser(userModel);
            modelList.get(i).setAddressId(6);
            modelList.get(i).setAddressText("肃北县");
        }
        int count = comRecruitersSubeiMapper.getModelListCount(model);
        ComRecruitersVo vo = new ComRecruitersVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    /**
     * 阿克塞
     *
     * @param model
     * @return
     */
    public ComRecruitersVo akesaiComRecruiters(ComRecruitersModel model) {
        List<ComRecruitersModel> modelList = comRecruitersAkesaiMapper.getComRecruitersList(model);
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setEnterprise(enterpriseAkesaiMapper.selectById(modelList.get(i).getComId()));
            if (modelList.get(i).getRRegisterState() == 4) {
                modelList.get(i).setStateText("已拒绝");
            } else if (modelList.get(i).getRRegisterState() == 3) {
                modelList.get(i).setStateText("待审核");
            } else if (modelList.get(i).getRRegisterState() == 9) {
                modelList.get(i).setStateText("已通过");
            }

            UserModel userModel = userAkesaiMapper.getModelByRid(modelList.get(i).getId());
            modelList.get(i).setUser(userModel);
            modelList.get(i).setAddressId(7);
            modelList.get(i).setAddressText("阿克塞县");
        }
        int count = comRecruitersAkesaiMapper.getModelListCount(model);
        ComRecruitersVo vo = new ComRecruitersVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    /**
     * 招聘人员花名册
     */
    @PostMapping({"/exportPerson"})
    @ResponseBody
    public void exportPerson(HttpServletResponse response, @RequestBody ComRecruitersModel comRecruitersModel) {
        Map<String, Object> map = Maps.newHashMap();
        comRecruitersModel.setPage(0);
        comRecruitersModel.setLimit(99999);
        if (comRecruitersModel.getAddressId() != null && comRecruitersModel.getAddressId() != 0) {
            if (comRecruitersModel.getAddressId() == 1) {
                ComRecruitersVo list = suzhouComRecruiters(comRecruitersModel);
                map.put("list", list.getList());
            } else if (comRecruitersModel.getAddressId() == 2) {
                ComRecruitersVo list = jintaComRecruiters(comRecruitersModel);
                map.put("list", list.getList());
            } else if (comRecruitersModel.getAddressId() == 3) {
                ComRecruitersVo list = dunhuangComRecruiters(comRecruitersModel);
                map.put("list", list.getList());
            } else if (comRecruitersModel.getAddressId() == 4) {
                ComRecruitersVo list = guazhouComRecruiters(comRecruitersModel);
                map.put("list", list.getList());
            } else if (comRecruitersModel.getAddressId() == 5) {
                ComRecruitersVo list = yumenComRecruiters(comRecruitersModel);
                map.put("list", list.getList());
            } else if (comRecruitersModel.getAddressId() == 6) {
                ComRecruitersVo list = subeiComRecruiters(comRecruitersModel);
                map.put("list", list.getList());
            } else if (comRecruitersModel.getAddressId() == 7) {
                ComRecruitersVo list = akesaiComRecruiters(comRecruitersModel);
                map.put("list", list.getList());
            }
        } else {
            List<ComRecruitersModel> list = new ArrayList<>();
            ComRecruitersVo list1 = suzhouComRecruiters(comRecruitersModel);
            ComRecruitersVo list2 = jintaComRecruiters(comRecruitersModel);
            ComRecruitersVo list3 = dunhuangComRecruiters(comRecruitersModel);
            ComRecruitersVo list4 = guazhouComRecruiters(comRecruitersModel);
            ComRecruitersVo list5 = yumenComRecruiters(comRecruitersModel);
            ComRecruitersVo list6 = subeiComRecruiters(comRecruitersModel);
            ComRecruitersVo list7 = akesaiComRecruiters(comRecruitersModel);
            list.addAll(list1.getList());
            list.addAll(list2.getList());
            list.addAll(list3.getList());
            list.addAll(list4.getList());
            list.addAll(list5.getList());
            list.addAll(list6.getList());
            list.addAll(list7.getList());
            map.put("list", list);
        }
        ClassPathResource classPathResource = new ClassPathResource("C:/jyx/酒泉市管理后台/招聘人员花名册.xlsx");
        String path = classPathResource.getPath();
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
        Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String fileName = "招聘人员花名册" + time + ".xlsx";
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
